package com.ruoyi.baseSys.service.impl;

import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.baseSys.domain.UserInfo;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.baseSys.mapper.MHouseholdInfoMapper;
import com.ruoyi.baseSys.domain.MHouseholdInfo;
import com.ruoyi.baseSys.service.IMHouseholdInfoService;

/**
 * 住户管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-25
 */
@Service
public class MHouseholdInfoServiceImpl implements IMHouseholdInfoService 
{
    @Autowired
    private MHouseholdInfoMapper mHouseholdInfoMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询住户管理
     * 
     * @param id 住户管理主键
     * @return 住户管理
     */
    @Override
    public MHouseholdInfo selectMHouseholdInfoById(Long id)
    {
        return mHouseholdInfoMapper.selectMHouseholdInfoById(id);
    }

    /**
     * 查询住户管理列表
     * 
     * @param mHouseholdInfo 住户管理
     * @return 住户管理
     */
    @Override
    public List<MHouseholdInfo> selectMHouseholdInfoList(MHouseholdInfo mHouseholdInfo)
    {
        return mHouseholdInfoMapper.selectMHouseholdInfoList(mHouseholdInfo);
    }

    /**
     * 新增住户管理
     * 
     * @param mHouseholdInfo 住户管理
     * @return 结果
     */
    @Override
    public int insertMHouseholdInfo(MHouseholdInfo mHouseholdInfo)
    {
        return mHouseholdInfoMapper.insertMHouseholdInfo(mHouseholdInfo);
    }

    /**
     * 修改住户管理
     * 
     * @param mHouseholdInfo 住户管理
     * @return 结果
     */
    @Override
    public int updateMHouseholdInfo(MHouseholdInfo mHouseholdInfo)
    {
        return mHouseholdInfoMapper.updateMHouseholdInfo(mHouseholdInfo);
    }

    /**
     * 批量删除住户管理
     * 
     * @param ids 需要删除的住户管理主键
     * @return 结果
     */
    @Override
    public int deleteMHouseholdInfoByIds(Long[] ids)
    {
        return mHouseholdInfoMapper.deleteMHouseholdInfoByIds(ids);
    }

    /**
     * 删除住户管理信息
     * 
     * @param id 住户管理主键
     * @return 结果
     */
    @Override
    public int deleteMHouseholdInfoById(Long id)
    {
        return mHouseholdInfoMapper.deleteMHouseholdInfoById(id);
    }

    //添加住户信息
    @Override
    public String adduserinfo(UserInfo userInfo) {
        SysUser sysUser=new  SysUser();


        sysUser.setUserName(userInfo.getName());
        sysUser.setNickName(userInfo.getName());
        sysUser.setPhonenumber(userInfo.getPhone());
        if (userInfo.getSex()==0){
            sysUser.setSex("男");
        }else {
            sysUser.setSex("女");
        }
        sysUser.setEmail(userInfo.getWeixin());

        mHouseholdInfoMapper.adduserinfo(userInfo);
        userInfo.setPassword("123456");
        String msg = "", username = userInfo.getName(), password = userInfo.getPassword();
        //用户注册信息验证
        if (StringUtils.isEmpty(username))
        {
            msg = "用户名不能为空";
        }
        else if (StringUtils.isEmpty(password))
        {
            msg = "用户密码不能为空";
        }
        else if (username.length() < UserConstants.USERNAME_MIN_LENGTH
                || username.length() > UserConstants.USERNAME_MAX_LENGTH)
        {
            msg = "账户长度必须在2到20个字符之间";
        }
        else if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
        {
            msg = "密码长度必须在5到20个字符之间";
        }
        else
        {
            userInfo.setPassword(SecurityUtils.encryptPassword(password));
            sysUser.setPassword(userInfo.getPassword());
            boolean regFlag = userService.registerUser(sysUser);
            if (!regFlag)
            {
                msg = "注册失败,请联系系统管理人员";
            }
            else
            {
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.register.success")));
            }
        }
        return msg;
    }
    //添加住户登录信息
}
